﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace GEF1.GEF
{
    public class EntityView: IEntityView
    {
        public int mX;
        public int mY;
        public int mWidth;
        public int mHeight;
        private int mLineWidth;
        private Form mCanvas;
        private System.Drawing.Rectangle mBoundingBox;

        public EntityView(Form form)
        {
            mCanvas = form;
            mLineWidth = 1;
        }

        public EntityView(Form form, int x, int y, int width, int height)
        {
            mCanvas = form;
            mLineWidth = 1;
            mX = x;
            mY = y;
            mWidth = width;
            mHeight = height;
            updateBoundingBox();
        }

        public int x
        {
            set
            { 
                mX = value; 
                updateBoundingBox();
            }
        }

        public virtual void draw()
        {
            using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, mLineWidth))
            {
                using (System.Drawing.Graphics graph = mCanvas.CreateGraphics())
                {
                    graph.DrawRectangle(pen, new System.Drawing.Rectangle(mX, mY, mWidth, mHeight));
                }
            }
        }

        public void select(bool selected)
        {
            mCanvas.Invalidate(mBoundingBox);
            mLineWidth = selected ? 10 : 1;
            updateBoundingBox();
            draw();
        }

        private void updateBoundingBox()
        {
            mBoundingBox = new System.Drawing.Rectangle(mX - mLineWidth / 2, mY - mLineWidth / 2, mWidth + mLineWidth, mHeight + mLineWidth);
        }

        public bool containsRect(System.Drawing.Rectangle rect)
        {
            return mBoundingBox.IntersectsWith(rect);
        }

        public bool containsPoint(System.Drawing.Point point)
        {
            return mBoundingBox.Contains(point);
        }

    }
}
